import numpy as np
from sklearn.linear_model import LogisticRegression

from Tools import metrics_result
from Tools import readbunchobj
import os
from joblib import dump
from sklearn.preprocessing import StandardScaler

# 导入训练集
trainpath = "../train_word_bag/tf_idf_space.dat"
train_set = readbunchobj(trainpath)

# 导入测试集
testpath = "../test_word_bag/test_tf_idf_space.dat"
test_set = readbunchobj(testpath)

# 输出单词矩阵的类型
print("标准化/归一化前的矩阵形状:")
print(np.shape(train_set.tdm))
print(np.shape(test_set.tdm))

# 对数据进行标准化或归一化
scaler = StandardScaler(with_mean=False)
train_set.tdm = scaler.fit_transform(train_set.tdm)
test_set.tdm = scaler.transform(test_set.tdm)

# 输出单词矩阵的类型
print("标准化/归一化后的矩阵形状:")
print(np.shape(train_set.tdm))
print(np.shape(test_set.tdm))

# 回归
print("\n开始训练回归模型...")
# C:正则化强度的逆。指定更强的正则化，这意味着模型会尝试减少权重的大小以避免过拟合。较大的C值指定较弱的正则化，这可能会导致模型更好地拟合训练数据，但也可能更容易过拟合。
# sag:随机平均梯度下降（Stochastic Average Gradient descent），它是一种适用于大规模和稀疏数据的优化算法。# max_iter:最大迭代次数为1000次
Log_clf = LogisticRegression(C=1000.0, solver='sag', max_iter=1000).fit(train_set.tdm, train_set.label)
print("回归模型训练完成。")
dump(Log_clf, 'LogisticRegression_model.joblib')

print("USE: 回归模型 预测分类结果")
Log_predicted = Log_clf.predict(test_set.tdm)
Log_total = len(Log_predicted)
print("结束")

print("\n回归模型：")
metrics_result(test_set.label, Log_predicted, Log_total)





